//
//  WebHtmlCSS.m
//  caishuitong
//
//  Created by 企友软mac on 2017/5/24.
//  Copyright © 2017年 zhangxy. All rights reserved.
//

#import "WebHtmlCSS.h"

@implementation WebHtmlCSS

// 格式化文字
+ (NSString *)formatTextHtml:(NSString *)htmlContent regex:(NSString *)regex{
    NSError *error;
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regex
                                                                             options:NSRegularExpressionCaseInsensitive
                                                                               error:&error];
    // 对str字符串进行匹配
    NSArray *matches = [regular matchesInString:htmlContent
                                        options:0
                                          range:NSMakeRange(0, htmlContent.length)];
    
    NSString *mStr;
    // 遍历匹配后的每一条记录
    for (NSTextCheckingResult *match in matches) {
        NSRange range = [match range];
        mStr = [htmlContent substringWithRange:range];
        WLog(@"%@", mStr);
    }
    
    if (mStr.length <= 0) {
        mStr = htmlContent;
    }
    
    return mStr;
}

//// 1 如果存在span标签
//+ (NSString *)spanFormatTextHtml:(NSString *)htmlContent{
//    
//    NSString *regex = @"<\\s*span\\s*([^\"']|\"[^\"]*\"|'[^']*')*?>";
//    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];
//
//}
//
//// 11 如果存在font标签
//+ (NSString *)fontlabelFormatTextHtml:(NSString *)htmlContent{
//    NSString *regex = @"<\\s*font\\s*([^\"']|\"[^\"]*\"|'[^']*')*?>";
//    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];
//    
//}
//
//// 12 如果存在a标签
//+ (NSString *)alabelFormatTextHtml:(NSString *)htmlContent{
//    NSString *regex = @"<\\s*a\\s*([^\"']|\"[^\"]*\"|'[^']*')*?>";
//    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];
//    
//}
//
//// 13 如果存在table标签
//+ (NSString *)tableFormatTextHtml:(NSString *)htmlContent{
//    NSString *regex = @"<\\s*table\\s*([^\"']|\"[^\"]*\"|'[^']*')*?>";
//    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];
//    
//}

//// 10 如果存在p标签
//+ (NSString *)pFormatTextHtml:(NSString *)htmlContent{
//    NSString *regex = @"<\\s*p\\s*([^\"']|\"[^\"]*\"|'[^']*')*?>";
//    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];
//    
//}


// 1 如果存在style
+ (NSString *)styleFormatTextHtml:(NSString *)htmlContent{
    NSString *regex = @"style\\s*=\\s*\"[^\"]*\"";
    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];

}

// 2 如果存在font-size
+ (NSString *)fontsizeFormatTextHtml:(NSString *)htmlContent{
    NSString *regex = @"\\s*font-size\\s*:";
    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];

}

// 3 如果存在font-color
+ (NSString *)fontcolorFormatTextHtml:(NSString *)htmlContent{
    NSString *regex = @"\\s*font-color\\s*:";
    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];

}

// 4 如果存在font
+ (NSString *)fontFormatTextHtml:(NSString *)htmlContent{
    NSString *regex = @"\\s*font\\s*:";
    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];

}

// 5 如果存在color
+ (NSString *)colorFormatTextHtml:(NSString *)htmlContent{
    NSString *regex = @"\\s*color\\s*:";
    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];

}

// 6 如果存在background-color
+ (NSString *)backgroundcolorFormatTextHtml:(NSString *)htmlContent{
    NSString *regex = @"\\s*background-color\\s*:";
    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];

}

// 7 如果存在text-indent
+ (NSString *)textindentFormatTextHtml:(NSString *)htmlContent{
    NSString *regex = @"\\s*text-indent\\s*:";
    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];

}

// 8 如果存在size
+ (NSString *)sizeFormatTextHtml:(NSString *)htmlContent{
    NSString *regex = @"\\s*size\\s*=";
    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];

}


// 9 如果存在href
//+ (NSString *)hrefFormatTextHtml:(NSString *)htmlContent{
//    NSString *regex = @"href\\s*=\\s*\"[^\"]*\"";
//    return [WebHtmlCSS formatTextHtml:htmlContent regex:regex];
//    
//}


// 最后的返回
+ (NSString *)allFormatTextHtml:(NSString *)htmlContent{
    NSString *str1 = [WebHtmlCSS styleFormatTextHtml:htmlContent];
    NSString *str2 = [WebHtmlCSS fontsizeFormatTextHtml:str1];
    NSString *str3 = [WebHtmlCSS fontcolorFormatTextHtml:str2];
    NSString *str4 = [WebHtmlCSS fontFormatTextHtml:str3];
    NSString *str5 = [WebHtmlCSS colorFormatTextHtml:str4];
    NSString *str6 = [WebHtmlCSS backgroundcolorFormatTextHtml:str5];
    NSString *str7 = [WebHtmlCSS textindentFormatTextHtml:str6];
    NSString *str8 = [WebHtmlCSS sizeFormatTextHtml:str7];
//    NSString *str9 = [WebHtmlCSS hrefFormatTextHtml:str8];

    return str8;
}

+ (NSString *)makeUpFormatTextHtmlWithCSS {
    int fontsize = 17;
    int line_height = 30;
    
    NSString *color = @"#333333";
    NSString *background_color = @"#ffffff";
    NSString *width = @"100%";
    NSString *textFont = [NSString stringWithFormat:@"<!DOCTYPE HTML> <html><meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no'><head><link rel='stylesheet' href='file:///android_asset/css/newsdetails.css' type='text/css'><style>body{font-size: %d888px;letter-spacing:1px; color:%@888;background-color:%@888;line-height:%d888px;}section{font-size: %d888px;letter-spacing:1px; color:%@888;background-color:%@888;line-height:%d888px;}p{font-size: %d888px;letter-spacing:1px; color:%@888;margin-top:10px;padding-bottom:10px;text-align:justify;}div{margin-top:10px;}img {width:%@;height:auto;padding:0px;margin:0px;}</style><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head><body>",fontsize,color,background_color,line_height,fontsize,color,background_color,line_height,fontsize,color,width];
    
    return textFont;
}

@end
